package com.cheyun.netsalev3.view.allocated

import android.content.Context
import android.content.Intent
import android.view.inputmethod.EditorInfo
import androidx.fragment.app.Fragment
import com.cheyun.netsalev3.R
import com.cheyun.netsalev3.adapter.HomePageAdapter
import com.cheyun.netsalev3.databinding.ActivityAllocatedBinding
import com.cheyun.netsalev3.utils.BaseActivity
import com.cheyun.netsalev3.utils.CommonFunc
import com.cheyun.netsalev3.view.SearchActivity
import android.content.Context.INPUT_METHOD_SERVICE
import android.util.Log
import android.view.inputmethod.InputMethodManager
import androidx.lifecycle.Observer
import com.jeremyliao.liveeventbus.LiveEventBus


class AllocatedActivity : BaseActivity<ActivityAllocatedBinding>() {


    override fun initContentView(): Int {
        return R.layout.activity_allocated
    }

    var titles = arrayListOf("全部", "未处理")
    var states = arrayListOf("", "0")

    private var today = ""
    private var searchMap = HashMap<String, String>()
    private var listFragmet = ArrayList<Fragment>()


    override fun initData() {
    }

    override fun initView() {
        for (i in titles.indices) {
            listFragmet.add(AllocatedFragment.newInstance(states[i]))
        }
        val pagerAdapter = HomePageAdapter(supportFragmentManager, listFragmet)
        pagerAdapter.titles = titles
        binding.viewPager.offscreenPageLimit = 2
        binding.viewPager.adapter = pagerAdapter
        binding.tabLayout.setupWithViewPager(binding.viewPager)
        binding.viewPager.setCurrentItem(1)

        binding.leftBut.setOnClickListener {
            this.finish()
        }

        binding.sV.setOnCheckedChangeListener { buttonView, isChecked ->
            today = if(isChecked) CommonFunc.getCurrentDate() else ""
            refresh()
        }

//        binding.searchText.setOnClickListener {
//            var intent = Intent(this, SearchActivity::class.java)
//            intent.putExtra("from", "allocated")
//            startActivity(intent)
//        }

        LiveEventBus.get()
                .with("allocatedsearch", HashMap::class.java)
                .observe(this, Observer {
                    searchMap = it as HashMap<String, String>
                    refresh()
                })
        binding.searchText.setOnEditorActionListener { v, actionId, event ->
            if (actionId == EditorInfo.IME_ACTION_SEARCH || actionId == EditorInfo.IME_ACTION_UNSPECIFIED) {
                //关闭软键盘
                closeKeyword()
                //do something
                //doSearch();
                searchMap = HashMap<String, String>()
                searchMap["keyword"] = binding.searchText.text.toString()

                refresh()
                true
            } else
                false
        }
    }

    fun refresh(){
        var hashMap = HashMap<String, String>()
        if(today.isNotEmpty()) {
            hashMap["stime"] = today
            hashMap["etime"] = today
            hashMap["datetype"] = "dateline"
        }
        searchMap.forEach {
            hashMap[it.key] = it.value
        }
        for (fragment in listFragmet) {
            (fragment as AllocatedFragment).setSearch(hashMap)
        }
    }

    fun closeKeyword(){
        val view = window.peekDecorView()
        if (view != null) {
            val inputmanger = this.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
            inputmanger.hideSoftInputFromWindow(view.windowToken, 0)
        }
    }

}
